package com.qqtech.qquan.order.controller;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.order.enums.OrderBaseOrderStatusDictEnum;
import com.qqtech.qquan.order.enums.OrderSplitTypeEnum;
import com.qqtech.qquan.order.model.OrderBase;
import com.qqtech.qquan.order.model.OrderBaseQuery;
import com.qqtech.qquan.order.model.OrderPayLog;
import com.qqtech.qquan.order.model.OrderSplit;
import com.qqtech.qquan.order.service.OrderBaseService;
import com.qqtech.qquan.order.service.OrderHandleService;
import com.qqtech.qquan.order.service.OrderSplitService;
import com.qqtech.qquan.sys.controller.BaseAgentController;

@Controller
@RequestMapping(value = "/order/base/task")
public class OrderTaskController extends BaseAgentController {

	@Resource
	private OrderSplitService orderSplitService;
	@Resource
	private OrderBaseService orderBaseService;
	@Resource
	private OrderHandleService orderHandleService;

	@Override
	protected BaseService<OrderBase> getBaseService() {
		return orderBaseService;
	}

	/**
	 * 初始化订单卖家总额
	 * 
	 * @param orderNo
	 * @return
	 */
	@RequestMapping(value = "/initSellerTotal")
	@ResponseBody
	public Result initSellerTotal(String orderNo) {
		int i = 0;
		Result result = new Result();
		List<OrderBase> all = orderBaseService.queryAll();
		for (OrderBase orderBase : all) {
			if (orderBase.getOrderStatus() == null
					|| orderBase.getOrderStatus().intValue() != OrderBaseOrderStatusDictEnum.COMPLETED.getCode()) {
				continue;
			}
			OrderSplit orderSplit = new OrderSplit();
			orderSplit.setOrderNo(orderBase.getOrderNo());
			List<OrderSplit> orderSplitList = orderSplitService.queryList(orderSplit);
			BigDecimal sellerTotal = BigDecimal.ZERO;
			if (orderSplitList != null && orderSplitList.size() > 0) {
				for (OrderSplit orderSplit2 : orderSplitList) {
					BigDecimal bv = orderSplit2.getValue();
					if (orderSplit2.getSellerId() != null && orderSplit2.getSellerId().intValue() != 0 && bv != null
							&& bv.compareTo(BigDecimal.ZERO) == 1) {
						if (orderSplit2.getType() != null) {
							if (OrderSplitTypeEnum.DELIVERY.getCode() == orderSplit2.getType().intValue()) {
							} else if (OrderSplitTypeEnum.SUBSIDY.getCode() == orderSplit2.getType().intValue()) {
								bv = BigDecimal.ZERO.subtract(bv);
							}
						}
						sellerTotal = sellerTotal.add(bv);
					}
				}
			} else {// 兼容以前(没分账功能的时候)的商家模式
				if (orderBase.getSellerId() != null && orderBase.getSellerId().intValue() != 0
						&& orderBase.getTotal() != null && orderBase.getTotal().compareTo(BigDecimal.ZERO) == 1) {
					sellerTotal = orderBase.getTotal();
				}
			}
			OrderBaseQuery orderBaseNew = new OrderBaseQuery();
			orderBaseNew.setId(orderBase.getId());
			orderBaseNew.setSellerTotal(sellerTotal);
			orderBaseService.updateSellerTotalById(orderBaseNew);
			i++;
		}
		result.put("message", "一共完成" + i + "个订单的卖家总额初始化");
		return result;
	}

	@RequestMapping(value = "/handleZeroOrder/{orderNo}")
	@ResponseBody
	public Result handleZeroOrder(@PathVariable String orderNo) {
		Result result = new Result();
		OrderPayLog orderPayLog = new OrderPayLog();
		orderPayLog.setOrderNo(orderNo);
		orderPayLog.setValue(BigDecimal.ZERO);
		orderPayLog.setPayWay(0);
		result = orderHandleService.payOrder(orderPayLog);
		result.put("message", "订单" + orderNo + "已处理完毕");
		return result;
	}
}
